<?php

namespace source\models;

use Yii;

use yii\db\Query;
use yii\db\Schema;
use yii\helpers\Url;
use yii\helpers\Html;

use source\aii;
use source\libs\Common;
use source\libs\Constants;
use source\helpers\StringHelper;
use source\helpers\DateTimeHelper;
use source\core\base\BaseActiveRecord;
use source\modules\post\models\ContentPost;
use source\core\behaviors\DefaultValueBehavior;
/**
 * This is the model class for table "{{%skypad_reports}}".
 *
 * @property integer $id
 * @property string $title
 * @property string $description
 * @property string $thumb
 * @property string $contents
 * @property string $view
 * @property integer $level
 * @property string $category_id
 * @property string $author
 * @property string $attachments
 * @property string $config_list
 * @property string $create_time
 * @property string $update_time
 */
class Report extends BaseActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%skypad_reports}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['title', 'thumb', 'view', 'attachments', 'create_time'], 'required'],
            [['contents', 'view'], 'string'],
            [['level'], 'integer'],
            [['title', 'description'], 'string', 'max' => 100],
            [['thumb', 'config_list'], 'string', 'max' => 200],
            [['category_id'], 'string', 'max' => 64],
            [['author'], 'string', 'max' => 20],
            [['attachments'], 'string', 'max' => 150],
            [['create_time', 'update_time'], 'string', 'max' => 11],
            [['title', 'level', 'category_id'], 'unique', 'targetAttribute' => ['title', 'level', 'category_id'], 'message' => 'The combination of 报告标题, 报告等级 and 报告类型 has already been taken.']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => '报告ID',
            'title' => '报告标题',
            'description' => '报告简介',
            'thumb' => '报告缩略图',
            'contents' => '报告内容',
            'view' => '报告样式',
            'level' => '报告等级',
            'category_id' => '报告类型',
            'author' => '报告编辑人',
            'attachments' => '报告附件id 列表',
            'config_list' => '报告小部件设置信息',
            'create_time' => '报告创建时间',
            'update_time' => '报告修改时间',
        ];
    }

    public function scenarios()
    {
        $parent = parent::scenarios();

        $parent['create']   =  ['title','description','thumb','contents','view','level'];
        $parent['delete']   =  ['id','title'];
        $parent['search']   =  ['title','contents'];
        $parent['update']   =  ['update_time'];
        $parent['download'] =  ['id'];

        return $parent;

    }

    public function lists($params=array())
    {
        if(!isset($params)||empty($params))
        {
            return -1 ; // null error
        }
        if(!is_array($params))
        {
            return -2; // type error
        }
        $query = null ;
        if(array_key_exists('time',$params))
        {
            if(!isset($params['opts']))
            {

            }
           // $query = $this->listByTime($params['time']);
        }
        if(array_key_exists('limit',$params))
        {

        }
        if(array_key_exists('range',$params))
        {

        }
        return -3; // unknown error
    }

    public function create()
    {

    }


    public function delete($params=array())
    {

    }


    public function searchByContent()
    {

    }

    public function searchByTitle()
    {

    }

    public function listByCategory()
    {

    }

    public function listByLevel()
    {

    }

    public function listByTime($time,$opts)
    {


    }

    public function listById($id,$opts)
    {

    }

    public function listByRange($range,$opts)
    {


    }

    public function option($value=null,$opt = true)
    {
        $fields = $this->attributeLabels();
        if(empty($value)){
            return   $fields;
        }
        if(is_string($value))
        {
            $ret = array_key_exists($value,$fields);
            if(!$opt){
                return $ret ;
            }
            if($ret){
                return $fields[$value];
            }
        }
        return null;
    }
}
